﻿package net.pezzato.grpbannr{
	import flash.display.Sprite;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.navigateToURL;
	import flash.display.Stage;
	import net.pezzato.grpbannr.Label;
	public class LinkBanner extends Sprite {
		private var url_:String;
		private var urlLabel_:Label;
		private var over_:Number;
		private var stage_:Stage;
		private var direction_:Number;
		public function LinkBanner(url:String, stage:Stage) {
			stage_ = stage;
			over_ = -0.1;
			direction_ = -1;
			url_ = url;
			urlLabel_ = new Label(url_);
			addEventListener(MouseEvent.CLICK, onMouseClick);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			buttonMode = true;
			useHandCursor = true;
			urlLabel_.y = (stage_.stageHeight - urlLabel_.height) / 2;
			addChild(urlLabel_);
			drawBorders();
			trace("LinkBanner created");
		}
		private function drawBorders() {
			graphics.beginFill(0xffffff, 0);
			graphics.drawRect(0, 0, stage_.stageWidth, stage_.stageHeight);
		}
		private function onMouseClick(event:MouseEvent) {
			var request:URLRequest = new URLRequest(url_);
			try {
				navigateToURL(request, '_blank');
			} catch (e:Error) {
				trace("Error occurred!");
			}
		}
		private function onMouseOver(event:MouseEvent) {
			over_ = +0.1;
		}
		private function onMouseOut(event:MouseEvent) {
			over_ = -0.1;
		}
		private function onEnterFrame(event:Event) {
			if ((urlLabel_.alpha < 1) && (over_ > 0)) {
				urlLabel_.alpha += 0.1;
			}
			if ((urlLabel_.alpha > 0) && (over_ < 0)) {
				urlLabel_.alpha -= 0.1;
			}
			if (direction_ < 0) {
				if (urlLabel_.x > stage_.stageWidth - urlLabel_.width) {
					urlLabel_.x += direction_;
				} else {
					direction_ *= -1;
				}
			} else {
				if (urlLabel_.x < 0) {
					urlLabel_.x += direction_;
				} else {
					direction_ *= -1;
				}
			}
		}
	}
}